package com.purvar.mybatis.util;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.purvar.exception.BusinessException;
import com.purvar.mybatis.util.bean.Relation;
import com.purvar.mybatis.util.enums.RelationType;
import com.purvar.mybatis.util.mapper.AutoMapper;
import com.purvar.util.SpringContextHolder;

import java.util.List;

/**
 * @author zcc
 * @version 1.0.0
 * @ClassName RelationHelper.java
 * @Description TODO
 * @createTime 2020年05月27日 15:43:00
 */
public class RelationHelper {


    public static <T> T render(T t) {
        return RelationHelper.render(t, null);
    }

    public static <T> T render(T t, QueryWrapper queryWrapper) {
        AutoMapper autoMapper = SpringContextHolder.getBean(AutoMapper.class);
        if (CollectionUtil.isEmpty(autoMapper.getRelationListMap())) {
            autoMapper.initData(autoMapper.getEntityPackages());
        }
        if (CollectionUtil.isEmpty(autoMapper.getRelationListMap())) {
            return t;
        }
        List<Relation> relations = autoMapper.getRelationListMap().get(t.getClass().getSimpleName());
        relations.stream().forEach(i -> {
            if (i.getRelationType() == RelationType.ONETOONE) {
                autoMapper.oneToOne(t, i, queryWrapper);
            } else if (i.getRelationType() == RelationType.ONETOMANY) {
                autoMapper.oneToMany(t, i, queryWrapper);
            } else if (i.getRelationType() == RelationType.MANYTOONE) {
                autoMapper.ManyToOne(t, i, queryWrapper);
            } else if (i.getRelationType() == RelationType.MANYTOMANY) {
                autoMapper.manyToMany(t, i, queryWrapper);
            }
        });
        return t;
    }


    public static <T> List<T> renderList(List<T> t) {
        return RelationHelper.renderList(t, null);
    }

    public static <T> List<T> renderList(List<T> t, QueryWrapper queryWrapper) {
        AutoMapper autoMapper = SpringContextHolder.getBean(AutoMapper.class);
        if (CollectionUtil.isEmpty(autoMapper.getRelationListMap())) {
            autoMapper.initData(autoMapper.getEntityPackages());
        }
        if (CollectionUtil.isEmpty(autoMapper.getRelationListMap()) || CollectionUtil.isEmpty(t)) {
            return t;
        }
        /* autoMapper.initData(autoMapper.getEntityPackages());*/
        List<Relation> relations = autoMapper.getRelationListMap().get(t.get(0).getClass().getSimpleName());
        if (relations == null) throw new BusinessException("找不到" + t.get(0).getClass().getSimpleName() + "bean配置关系");
        relations.stream().forEach(i -> {
            if (i.getRelationType() == RelationType.ONETOONE) {
                autoMapper.oneToOne(t, i, queryWrapper);
            } else if (i.getRelationType() == RelationType.ONETOMANY) {
                autoMapper.oneToMany(t, i, queryWrapper);
            } else if (i.getRelationType() == RelationType.MANYTOONE) {
                autoMapper.ManyToOne(t, i, queryWrapper);
            } else if (i.getRelationType() == RelationType.MANYTOMANY) {
                autoMapper.manyToMany(t, i, queryWrapper);
            }
        });
        return t;
    }

}
